﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmUserGroup : Office2007Form
    {
        #region Fields
        UserGroupCtrl m_UserGroupCtrl = new UserGroupCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmUserGroup()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmUserGroup_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_UserGroupCtrl.Show(dgvGroup, bindingNavigatorGroup, txtGroupId, txtGroupName, chkDefault, chkAdmin, chkMod, chkDelete, chkEdit, chkReport, txtRemark);
        }
        #endregion

        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            UserGroupInfo info = new UserGroupInfo();
            info.UserGroupId = txtGroupId.Text;
            info.UserGroupName = txtGroupName.Text;
            info.IsDefault = chkDefault.Checked;
            info.IsAdmin = chkDefault.Checked;
            info.IsMod = chkMod.Checked;
            info.CanEdit = chkEdit.Checked;
            info.CanDelete = chkDelete.Checked;
            info.CanReport = chkReport.Checked;
            info.Remark = txtRemark.Text;
            if (isNew == true)
            {
                m_UserGroupCtrl.Add(info);
            }
            else
            {
                m_UserGroupCtrl.Update(info);
            }
            m_UserGroupCtrl.Show(dgvGroup, bindingNavigatorGroup, txtGroupId, txtGroupName, chkDefault, chkAdmin, chkMod, chkDelete, chkEdit, chkReport, txtRemark);
            SaveItem.Enabled = false;
            isNew = false;
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_UserGroupCtrl.Show(dgvGroup, bindingNavigatorGroup, txtGroupId, txtGroupName, chkDefault, chkAdmin, chkMod, chkDelete, chkEdit, chkReport, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvGroup.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_UserGroupCtrl.AddNewRow();
            m_Row["UserGroupId"] = GetID.UserGroup(dgvGroup);
            m_Row["UserGroupName"] = "";
            m_Row["IsDefault"] = false;
            m_Row["IsAdmin"] = false;
            m_Row["IsMod"] = false;
            m_Row["CanDelete"] = false;
            m_Row["CanEdit"] = false;
            m_Row["CanReport"] = false;
            m_Row["Remark"] = "";
            m_UserGroupCtrl.AddNewGroup(m_Row);
            bindingNavigatorGroup.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_UserGroupCtrl.Show(dgvGroup, bindingNavigatorGroup, txtGroupId, txtGroupName, chkDefault, chkAdmin, chkMod, chkDelete, chkEdit, chkReport, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvGroup.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvGroup.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvGroup.SelectedRows)
                    {
                        bindingNavigatorGroup.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvGroup.Rows)
            {
                if (row.Cells["colUserGroupId"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_UserGroupCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ. Mã nhóm không được để trống !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region DataGridView Event
        private void dgvGroup_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }
        #endregion
        
        #region Searching
        private void rbGroupId_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Mã nhóm :";
        }

        private void rbGroupName_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Tên nhóm :";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (rbGroupId.Checked == true)
                {
                    dt = m_UserGroupCtrl.GetByLikeId(txtKeyWord.Text);
                }
                else if (rbGroupName.Checked == true)
                {
                    dt = m_UserGroupCtrl.GetByName(txtKeyWord.Text);
                }
                else
                {
                    dt = m_UserGroupCtrl.GetByName(txtKeyWord.Text);
                }
                m_UserGroupCtrl.ShowWithSources(dgvGroup, bindingNavigatorGroup, txtGroupId, txtGroupName, chkDefault, chkAdmin, chkMod, chkDelete, chkEdit, chkReport, txtRemark, dt);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region Context Menu Event Click
        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
